/** Description of Login.java 
 * @version 1.0
 * 
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.start;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.panther.awp.android.smoker.R;

public class Login extends Activity {
	
	private TextView welcomeText;
	private EditText passwordText;
	private Button loginButton;
	private Button discardButton;
	
	private String password;
	
	private static int LOGIN_REQUEST_CODE = 99;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
	    Boolean isProtectionEnabled = prefs.getBoolean("isPassEnabled", false);
	    if(!isProtectionEnabled) {
	    	Intent startAppIntent = new Intent(this, SmokersTabs.class);
	    	startActivityForResult(startAppIntent, LOGIN_REQUEST_CODE);
	    } else {
			setContentView(R.layout.login);
			welcomeText = (TextView) findViewById(R.id.userName);
			passwordText = (EditText) findViewById(R.id.passwordView);
			loginButton = (Button) findViewById(R.id.loginButton);
			discardButton = (Button) findViewById(R.id.discardButton);
			String userFName = prefs.getString("fName", "");
			welcomeText.setText(welcomeText.getText() + userFName);
			password = prefs.getString("lockPass", "Set some password");
			loginButton.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					if(passwordText.getText().toString().equals(password)) {
						Intent startAppIntent = new Intent(Login.this, SmokersTabs.class);
				    	startActivityForResult(startAppIntent, LOGIN_REQUEST_CODE);
					} else {
						Toast toast = Toast.makeText(Login.this, "Wrong Password", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM, 0, 20);
						toast.show();
					}
				}
			});
			discardButton.setOnClickListener(new View.OnClickListener() {
				
				public void onClick(View v) {
					finish();
				}
			});
	    }
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		finish();
	}

}
